package com.mlm.mlmapp.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.AQProgressDialog;
import com.mlm.mlmapp.adapter.view.AppointmentHuliDialog;
import com.mlm.mlmapp.adapter.view.AppointmentRanfaDialog;
import com.mlm.mlmapp.adapter.view.AppointmentTangfaDialog;
import com.mlm.mlmapp.adapter.view.AppointmentYangfaDialog;
import com.mlm.mlmapp.adapter.view.listener.DialogListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.DrawableRes;
import org.androidannotations.annotations.res.StringRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.book)
public class BookActivity extends BaseActivity implements HTTPCons {
    AQuery aq;
    AjaxCallback<JSONObject> getBookInfoCB;
    AppointmentTangfaDialog dialogTangfa;
    AppointmentRanfaDialog dialogRanfa;
    AppointmentHuliDialog dialogHuli;
    AppointmentYangfaDialog dialogYangfa;
    List<JSONObject> comboList;
    String dresserid;
    String currentName;
    String currentValue;

    JSONObject displayJson = new JSONObject();
    @StringRes
    String money;
    @Pref
    Prefs_ pref;
    @Extra
    String id;
    @Extra
    String styleid = "";
    @Extra
    String couponid;
    @ViewById
    ImageView btn_headimg;
    @ViewById
    TextView txt_1;
    @ViewById
    Button lable;
    @ViewById
    LinearLayout items;
    @ViewById
    Button btn_date;
    @ViewById
    Button btn_studio;
    @ViewById
    Button btn_group;
    @ViewById
    Button btn_ranfa;
    @ViewById
    Button btn_huli;
    @ViewById
    Button btn_yangfa;
    @ViewById
    Button btn_tangfa;
    @ViewById
    TextView tv_money;
    @ViewById
    TextView btn_next;
    @ViewById
    LinearLayout pros;
    @ViewById
    RelativeLayout tangfa;
    @ViewById
    RelativeLayout rl_combo;
    @ViewById
    LinearLayout ll_combo;
    @ViewById
    Button btn_tangfa_off;
    @ViewById
    Button btn_ranfa_off;
    @ViewById
    Button btn_huli_off;
    @ViewById
    RelativeLayout ranfa;
    @ViewById
    Button btn_yangfa_off;
    @ViewById
    RelativeLayout yangfa;
    @ViewById
    RelativeLayout huli;
    List<String> itemIDs;
    List<RadioButton> radioGroup;
    LayoutInflater inflater;
    @ViewById
    ImageButton cancel_combo;
    @ViewById
    TextView lable_combo_title;
    @ColorRes
    int red;
    @ColorRes
    int blacktext;
    @ColorRes
    int greyborder;
    @ColorRes
    int lightgreytext;
    @DrawableRes
    Drawable red_edge_btn_long;
    private String orderid;
    private final static int TYPE_BASE = 0;
    private final static int TYPE_TANGFA = 1;
    private final static int TYPE_RANFA = 2;
    private final static int TYPE_HULI = 3;
    private final static int TYPE_YANGFA = 4;
    private boolean isComboContainBaseType = false;
    private String dateid;
    private int total = 0;
    private int baseMoney = 0;
    private int ranfaMoney = 0;
    private int tangfaMoney = 0;
    private int huliMoney = 0;
    private int yangfaMoney = 0;
    private int comboMoney = 0;
    private String comboID = "";
    private String levelid;
    private String tangfaid = "";
    private String ranfaid = "";
    private String huliid = "";
    private String baseid = "";
    private List<Button> textGroup;
    private CompoundButton currChecked;
    private String studioid;
    boolean isCouponUsed = false;
    private String yangfaid = "";

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String title = getTitle().toString();
        if (!"".equals(title)) {
            ((TextView) this.findViewById(R.id.tv_title)).setText(title);
        }

        setIntent(intent); // 这一句必须的，否则Intent无法获得最新的数据
        this.id = intent.getStringExtra("id");
        getBookInfo();
    }

    @OnActivityResult(REQUEST_CODE_PAYMENT)
    void onPaymentResult(int resultcode, Intent data) {
//		if (resultcode == RESULT_CANCELED) {
//			return;
//		}
//		if (data != null) {
//			if (data.getBooleanExtra("flag", false)) {
//				orderBackAction();
//			} else {
//
//			}
//		}
        //// TODO: 2016/3/10
//		orderBackAction();

    }

    @OnActivityResult(REQUEST_CODE)
    void onResult(Intent data) {
        if (data != null) {
            if (data.getBooleanExtra("flag", false)) {
                init();
            } else {
                this.finish();
            }
        }
    }

    @AfterViews
    void init() {
        if (couponid != null && !"".equals(couponid) && !"0".equals(couponid)) {
            isCouponUsed = true;
        }
        String title = getTitle().toString();
        if (!"".equals(title)) {
            ((TextView) this.findViewById(R.id.tv_title)).setText(title);
        }

        if (id == null) {
            id = "";
        }
        if (styleid == null) {
            styleid = "";
        }
        if ("0".equals(pref.loginID().get())) {
            LoginActivity_.intent(this).startForResult(REQUEST_CODE);
            return;
        }
        aq = new AQuery(this);
        itemIDs = new ArrayList<String>();
        inflater = LayoutInflater.from(this);

        View view = inflater.inflate(R.layout.book_main_item_view, null);
        final Button b = (Button) view.findViewById(R.id.btn_name);
        b.setText(getString(R.string.money, 0));
        RadioButton rb = (RadioButton) view.findViewById(R.id.radio_main);
        rb.setText("洗剪吹");
        rb.setEnabled(false);
        items.addView(view);

        View view1 = inflater.inflate(R.layout.book_main_item_view, null);
        final Button b1 = (Button) view1.findViewById(R.id.btn_name);
        b1.setText(getString(R.string.money, 0));
        RadioButton rb1 = (RadioButton) view1.findViewById(R.id.radio_main);
        rb1.setEnabled(false);
        rb1.setText("洗吹");
        items.addView(view1);


        radioGroup = new ArrayList<RadioButton>();
        textGroup = new ArrayList<Button>();
        comboList = new ArrayList<>();
        tv_money.setText(getString(R.string.money, total));
        getBookInfoCB = new AjaxCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject html, AjaxStatus status) {
                if (status.getCode() == -101) {
                    Toast.makeText(aq.getContext(), "网络异常,请检查网络",
                            Toast.LENGTH_SHORT);
                    return;
                }
                if (status.getCode() == -103) {
                    Toast.makeText(aq.getContext(), "用户信息异常，请联系客服",
                            Toast.LENGTH_LONG).show();
                } else {
                    if (status.getCode() != 200) {
                        Toast.makeText(aq.getContext(),
                                "网络异常:" + status.getCode(), Toast.LENGTH_LONG)
                                .show();
                    } else {
                        try {
                            onSuccess(html);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        if (!"".equals(id))
            getBookInfo();
    }

    @OnActivityResult(REQUEST_CODE_COMBO)
    void onResultCOMBO(int resultcode, Intent data) {
        if (resultcode == RESULT_CANCELED) {
            return;
        }
        if (data != null) {
            try {
                clearCombo();
                JSONObject json = new JSONObject(
                        data.getStringExtra("jsonString"));
                int comboValue = getJsonInt(json, "comboValues");
                String comboid = getJsonString(json, "id");
                String comboName = getJsonString(json, "names");
                JSONArray array = getJsonArray(json, "comboDetails");
                this.comboID = comboid;
                btn_group.setText(getString(R.string.money, comboValue));
                btn_group.setTextColor(red);
                lable_combo_title.setText(comboName);
                comboMoney = comboValue;
                tv_money.setText(getString(R.string.money, getTotal()));

                ll_combo.removeAllViews();
                comboList.clear();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    comboList.add(obj);
                    JSONObject product = getJsonObject(obj, "productInfo");
                    String productName = getJsonString(product, "productName");
                    String productType = getJsonString(product, "productType");
                    String productClas = getJsonString(product, "productClas");
                    View v = inflater.inflate(R.layout.book_combo_group_item,
                            null);
                    TextView tv = (TextView) v
                            .findViewById(R.id.row_group_item);
                    tv.setText(productName);
                    ll_combo.addView(v);
                }
                rl_combo.setVisibility(View.VISIBLE);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            comboSelected();
        }
    }

    @OnActivityResult(REQUEST_CODE_DATE)
    void onResultDate(int resultcode, Intent data) {
        if (resultcode == RESULT_OK) {
            if (data != null) {
                dateid = data.getStringExtra("dateid");
                String date = data.getStringExtra("date");
                if (!"".equals(date) && !"".equals(dateid)) {
                    btn_date.setText(date);
                    try {
                        displayJson.put("appointTimes", date);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }

    /**
     * send sms authcode request
     *
     * @throws UnsupportedEncodingException
     */
    private void getBookInfo() {

        HashMap<String, String> map = new HashMap<>();
        map.put("stylistinfoId", id);
        if (isCouponUsed)
            map.put("userCouponId", couponid);
        aq.progress(new AQProgressDialog(this)).ajax(CommonUtils.getURLStringParams(get_book_info_action, map),
                JSONObject.class, getBookInfoCB);
    }

    @Click
    void btn_date() {
        DatePickerActivity_.intent(this).extra("id", id)
                .extra("dateid", dateid).extra("couponid", couponid).startForResult(REQUEST_CODE_DATE);
    }

    void applyAllowed() {
        btn_next.setTextColor(red);
        btn_next.setBackground(red_edge_btn_long);
    }

    void onSuccess(JSONObject obj) throws JSONException {
        Log.d("sort", "onSuccess: "+obj);
        this.displayJson = obj;
        try {
            this.displayJson = getJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        aq.id(btn_headimg).image(getJsonString(obj, "iconPhotoUrl"), true,
                true, 0, 0, null, Constants.FADE_IN);

        this.levelid = getJsonString(obj, "leveId");
        txt_1.setText(getJsonString(obj, "name"));
        lable.setText(getJsonString(obj, "leveName"));

        if (!"0".equals(getJsonString(obj, "couponValues"))) {
            tv_money.setText(getJsonString(obj, "couponValues"));
        }

        JSONArray array = getJsonArray(obj, "bookProducts");
        items.removeAllViews();
        itemIDs.clear();
        for (int i = 0; i < array.length(); i++) {
            final JSONObject temp = array.getJSONObject(i);
            View view = inflater.inflate(R.layout.book_main_item_view, null);
            final Button b = (Button) view.findViewById(R.id.btn_name);
            final Button cut = (Button) view.findViewById(R.id.btn_off);
            final TextView v = (TextView) view.findViewById(R.id.orginalprice);
            int oMoney = getJsonInt(temp, "originalPrice");
            int money = getJsonInt(temp, "price");
            v.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            if (oMoney > money) {
                v.setText(getString(R.string.money, oMoney));
            }
            b.setText(getString(R.string.money, money));
            RadioButton rb = (RadioButton) view.findViewById(R.id.radio_main);
            rb.setText(getJsonString(temp, "productName"));
            if (getJsonInt(temp, "isChecked") == 1) {
                rb.setChecked(true);

            } else {
                rb.setChecked(false);
            }

            if (getJsonInt(temp, "cutvalues") == 0) {
                cut.setVisibility(View.GONE);
            } else {
                cut.setVisibility(View.VISIBLE);
                cut.setText(getString(R.string.off,
                        getJsonInt(temp, "cutvalues")));
            }
            if (!isCouponUsed) {
                rb.setTag(i);
                rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        // TODO Auto-generated method stub

                        if (isComboContainBaseType) {
                            return;
                        }
                        if (isChecked) {
                            baseid = getJsonString(temp, "productId");
                            radioGroupChecked(buttonView);
                            itemIDs.add(getJsonString(temp, "productId"));
                            currentValue = b.getText().toString();
                            b.setTextColor(red);
                            currentName = buttonView.getText().toString();
                            buttonView.setTextColor(red);
                            baseMoney = getJsonInt(temp, "price");
                            int cutvalue = getJsonInt(temp, "cutvalues");
                            if (cutvalue > 0)
                                baseMoney = baseMoney - cutvalue;
                            if (!isCouponUsed)
                                tv_money.setText(getString(R.string.money,
                                        getTotal()));
                            currChecked = buttonView;
                        } else {
                            itemIDs.remove(getJsonString(temp, "productId"));
                            b.setTextColor(blacktext);
                            buttonView.setTextColor(blacktext);
                        }

                    }
                });
//				if (i == 0) {
//					rb.setChecked(true);
//				} else {
//					rb.setChecked(false);
//				}
                textGroup.add(b);
                radioGroup.add(rb);
            } else {
                rb.setEnabled(false);
                rb.setTextColor(lightgreytext);
                b.setTextColor(lightgreytext);
            }
            items.addView(view);
        }
        JSONArray typeList = getJsonArray(obj, "typeList");
        for (int i = 0; i < typeList.length(); i++) {
            JSONObject type = typeList.getJSONObject(i);
            String productName = getJsonString(type, "productName");
            String productId = getJsonString(type, "productId");
            int isChecked = getJsonInt(type, "isChecked");
            if ("护理".equals(getJsonString(type, "typeName"))) {
                if (getJsonInt(type, "cutvalues") == 0) {
                    btn_huli_off.setVisibility(View.INVISIBLE);
                } else {
                    btn_huli_off.setVisibility(View.VISIBLE);
                    btn_huli_off.setText(getString(R.string.off,
                            getJsonInt(type, "cutvalues")));
                }

                if (productName != null && !"".equals(productName) && isChecked == 1) {
                    btn_huli.setText(productName);
                    huliid = productId;
                }

            }
            if ("养发".equals(getJsonString(type, "typeName"))) {
                if (getJsonInt(type, "cutvalues") == 0) {
                    btn_yangfa_off.setVisibility(View.INVISIBLE);
                } else {
                    btn_yangfa_off.setVisibility(View.VISIBLE);
                    btn_yangfa_off.setText(getString(R.string.off,
                            getJsonInt(type, "cutvalues")));
                }

                if (productName != null && !"".equals(productName) && isChecked == 1) {
                    btn_yangfa.setText(productName);
                    yangfaid = productId;
                }

            }
            if ("染发".equals(getJsonString(type, "typeName"))) {

                if (getJsonInt(type, "cutvalues") == 0) {
                    btn_ranfa_off.setVisibility(View.INVISIBLE);
                } else {
                    btn_ranfa_off.setVisibility(View.VISIBLE);
                    btn_ranfa_off.setText(getString(R.string.off,
                            getJsonInt(type, "cutvalues")));
                }

                if (productName != null && !"".equals(productName) && isChecked == 1) {
                    btn_ranfa.setText(productName);
                    ranfaid = productId;
                }
            }
            if ("烫发".equals(getJsonString(type, "typeName"))) {

                if (getJsonInt(type, "cutvalues") == 0) {
                    btn_tangfa_off.setVisibility(View.INVISIBLE);
                } else {
                    btn_tangfa_off.setVisibility(View.VISIBLE);
                    btn_tangfa_off.setText(getString(R.string.off,
                            getJsonInt(type, "cutvalues")));
                }

                if (productName != null && !"".equals(productName) && isChecked == 1) {
                    btn_tangfa.setText(productName);
                    tangfaid = productId;
                }
            }

        }
        JSONObject studio = getJsonObject(obj, "studio");
        studioid = getJsonString(studio, "id");

        btn_studio.setText(getJsonString(studio, "names"));
        applyAllowed();
        if (isCouponUsed) {
            btn_tangfa.setEnabled(false);
            btn_ranfa.setEnabled(false);
            btn_huli.setEnabled(false);
            btn_yangfa.setEnabled(false);
            btn_group.setEnabled(false);
            lable.setEnabled(false);

            btn_headimg.setEnabled(false);
        } else {
            btn_tangfa.setEnabled(true);
            btn_ranfa.setEnabled(true);
            btn_huli.setEnabled(true);
            btn_yangfa.setEnabled(true);
            btn_group.setEnabled(true);
            lable.setEnabled(true);
            btn_headimg.setEnabled(true);
        }
    }

    private void radioGroupChecked(CompoundButton buttonView) {
        for (RadioButton rb : radioGroup) {
            if ((int) rb.getTag() != (int) buttonView.getTag()) {
                rb.setChecked(false);
            }

        }
    }

    private void comboSelected() {
        for (JSONObject obj : comboList) {
            JSONObject json = getJsonObject(obj, "productInfo");
            int type = getJsonInt(json, "productType");
            String productID = getJsonString(json, "id");
            switch (type) {
                case TYPE_BASE:
                    isComboContainBaseType = true;
                    baseid = productID;
                    baseMoney = 0;
                    diabledBaseStyle();
                    break;
                case TYPE_TANGFA:
                    tangfaid = productID;
                    tangfa.setVisibility(View.GONE);
                    btn_tangfa.setText("请选择");
                    tangfaMoney = 0;
                    break;
                case TYPE_RANFA:
                    ranfaid = productID;
                    ranfa.setVisibility(View.GONE);
                    btn_ranfa.setText("请选择");
                    ranfaMoney = 0;
                    break;
                case TYPE_HULI:
                    huliid = productID;
                    huliMoney = 0;
                    btn_huli.setText("请选择");
                    huli.setVisibility(View.GONE);
                    break;
                case TYPE_YANGFA:
                    yangfaid = productID;
                    yangfaMoney = 0;
                    btn_yangfa.setText("请选择");
                    yangfa.setVisibility(View.GONE);
                    break;
                default:

                    break;
            }
        }
        tv_money.setText(getString(R.string.money, getTotal()));
    }

    private void clearCombo() {
        isComboContainBaseType = false;
        tangfa.setVisibility(View.VISIBLE);
        ranfa.setVisibility(View.VISIBLE);
        huli.setVisibility(View.VISIBLE);
        yangfa.setVisibility(View.VISIBLE);
        enabledBaseStyle();
        btn_group.setText("请选择");
        btn_group.setTextColor(lightgreytext);

    }

    private void diabledBaseStyle() {
        for (int i = 0; i < textGroup.size(); i++) {
            textGroup.get(i).setTextColor(greyborder);
            radioGroup.get(i).setTextColor(greyborder);
            radioGroup.get(i).setChecked(false);
            radioGroup.get(i).setClickable(false);
        }
    }

    private void enabledBaseStyle() {
        for (int i = 0; i < textGroup.size(); i++) {
            textGroup.get(i).setTextColor(blacktext);
            radioGroup.get(i).setTextColor(blacktext);
            radioGroup.get(i).setClickable(true);
            if (currChecked != null) {
                if (currChecked.isChecked()) {
                    currChecked.setChecked(false);
                }
                currChecked.setChecked(true);
            }
        }
    }

    @Click
    void btn_next() {

        if(dateid!=null&&!dateid.equals("")) {
            submitAction();
        }else{
            Toast.makeText(this, "请选择预约时间!",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Click
    void cancel_combo() {
        comboID = "";
        huliid = "";
        ranfaid = "";
        tangfaid = "";
        yangfaid = "";
        rl_combo.setVisibility(View.GONE);
        comboMoney = 0;
        tv_money.setText(getString(R.string.money, getTotal()));
        clearCombo();
    }

    @Click
    void btn_tangfa() {
        dialogTangfa = new AppointmentTangfaDialog(this, levelid,
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub
                        dialogTangfa.dismiss();
                    }

                    @Override
                    public void onApply(JSONObject obj, String clazz) {
                        // TODO Auto-generated method stub
                        tangfaid = CommonUtils.getJsonString(obj, "productId");
                        btn_tangfa.setText(getCallbackText(
                                clazz,
                                CommonUtils.getJsonString(obj, "productName"),
                                getString(R.string.money,
                                        CommonUtils.getJsonInt(obj, "price"))));
                        tangfaMoney = CommonUtils.getJsonInt(obj, "price");
                        tv_money.setText(getString(R.string.money, getTotal()));
                        dialogTangfa.dismiss();
                    }
                });
        dialogTangfa.show();
    }

    @Click
    void btn_ranfa() {
        dialogRanfa = new AppointmentRanfaDialog(this, levelid,
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub
                        dialogRanfa.dismiss();
                    }

                    @Override
                    public void onApply(JSONObject obj, String clazz) {
                        // TODO Auto-generated method stub
                        ranfaid = CommonUtils.getJsonString(obj, "productId");
                        btn_ranfa.setText(getCallbackText(
                                clazz,
                                CommonUtils.getJsonString(obj, "productName"),
                                getString(R.string.money,
                                        CommonUtils.getJsonInt(obj, "price"))));
                        total += CommonUtils.getJsonInt(obj, "price");
                        ranfaMoney = CommonUtils.getJsonInt(obj, "price");
                        tv_money.setText(getString(R.string.money, getTotal()));
                        dialogRanfa.dismiss();
                    }
                });
        dialogRanfa.show();
    }

    @Click
    void btn_huli() {
        dialogHuli = new AppointmentHuliDialog(this, levelid,
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub
                        dialogHuli.dismiss();
                    }

                    @Override
                    public void onApply(JSONObject obj, String clazz) {
                        // TODO Auto-generated method stub
                        huliid = CommonUtils.getJsonString(obj, "productId");
                        btn_huli.setText(getCallbackText(
                                clazz,
                                CommonUtils.getJsonString(obj, "productName"),
                                getString(R.string.money,
                                        CommonUtils.getJsonInt(obj, "price"))));
                        huliMoney = CommonUtils.getJsonInt(obj, "price");
                        tv_money.setText(getString(R.string.money, getTotal()));
                        dialogHuli.dismiss();
                    }
                });
        dialogHuli.show();
    }

    @Click
    void btn_yangfa() {
        dialogYangfa = new AppointmentYangfaDialog(this, levelid,
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub
                        dialogYangfa.dismiss();
                    }

                    @Override
                    public void onApply(JSONObject obj, String clazz) {
                        // TODO Auto-generated method stub
                        yangfaid = CommonUtils.getJsonString(obj, "productId");
                        btn_yangfa.setText(getCallbackText(
                                clazz,
                                CommonUtils.getJsonString(obj, "productName"),
                                getString(R.string.money,
                                        CommonUtils.getJsonInt(obj, "price"))));
                        yangfaMoney = CommonUtils.getJsonInt(obj, "price");
                        tv_money.setText(getString(R.string.money, getTotal()));
                        dialogYangfa.dismiss();
                    }
                });
        dialogYangfa.show();
    }

    private int getTotal() {
        total = baseMoney + comboMoney + ranfaMoney + tangfaMoney + huliMoney + yangfaMoney;
        return total;
    }

    @Click
    void btn_group() {
        ComboListActivity_.intent(this).extra("comboid", levelid)
                .startForResult(REQUEST_CODE_COMBO);
    }

    @Click
    void lable() {
        HairDresserListActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
    }
//	private void orderBackAction() {
//		AjaxCallback<JSONObject> submitCallback = new AjaxCallback<JSONObject>() {
//
//			@Override
//			public void callback(String url, JSONObject html, AjaxStatus status) {
//				if (status.getCode() == -101) {
//					Toast.makeText(aq.getContext(), "网络异常,请检查网络",
//							Toast.LENGTH_SHORT);
//					return;
//				}
//				if (status.getCode() != 200) {
//					Toast.makeText(aq.getContext(), "网络异常:" + status.getCode(),
//							Toast.LENGTH_LONG).show();
//				} else {
//					try {
//						if (html.getBoolean("isSuccess")) {
//
//							// MainActivity_.intent(LoginActivity.this).start();
//
//							orderid = getJsonArray(html, "errors").getString(0);
//						} else {
//							Toast.makeText(aq.getContext(),
//									html.getString("errors"), Toast.LENGTH_LONG)
//									.show();
//						}
//					} catch (JSONException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//
//				}
//			}
//		};
//		String mobile = pref.loginID().get();
//		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
//
//		pairs.add(new BasicNameValuePair("mobile", mobile));
//		pairs.add(new BasicNameValuePair("orderId", id));
//
//		CommonUtils.postRequest(book_order_back_action, aq, submitCallback, pairs);
//
//	}

    JSONObject getJson() throws Exception {
        JSONObject object = new JSONObject();
        object.put("iconPhotoUrl", getJsonString(displayJson, "iconPhotoUrl"));
        object.put("name", getJsonString(displayJson, "name"));
        object.put("leveName", getJsonString(displayJson, "leveName"));
        object.put("studio", getJsonObject(displayJson, "studio"));


        JSONArray arr = new JSONArray();
        for (int i = 0; i < 5; i++) {
            JSONObject ob = new JSONObject();
            ob.put("productName", "");
            ob.put("price", "");
        }
        object.put("bookProducts", arr);

        return object;
    }

    private void submitAction() {
        AjaxCallback<JSONObject> submitCallback = new AjaxCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject html, AjaxStatus status) {
                Log.d("sort", "callback: " + url + html + status.getError());
                if (status.getCode() == -101) {
                    Toast.makeText(aq.getContext(), "网络异常,请检查网络",
                            Toast.LENGTH_SHORT);
                    return;
                }
                if (status.getCode() != 200) {
                    Toast.makeText(aq.getContext(), "网络异常:" + status.getCode(),
                            Toast.LENGTH_LONG).show();
                } else {
                    try {
                            if(html.isNull("integral")){
                                Toast.makeText(aq.getContext(), "请联系客服:" + status.getCode(),
                                        Toast.LENGTH_LONG).show();
                                return ;
                            }
//                        if (html.getBoolean("isSuccess")) {

                            // MainActivity_.intent(LoginActivity.this).start();

//							orderid = getJsonArray(html, "errors").getString(0);
                            displayJson.put("integralMoney", getJsonString(html, "integralMoney"));
                            displayJson.put("integral", getJsonString(html, "integral"));
                            displayJson.put("orderMoney", getTotal());
                            JSONArray jsonArr = new JSONArray();
                            JSONObject product;
                            if (currentName != null && !currentName.equals("")) {
                                product = new JSONObject();
                                product.put("productName", currentName);
                                product.put("price", currentValue);
                                jsonArr.put(product);
                            }
                            String[] arr = null;
                            if (btn_tangfa.getText().toString().contains(" ")) {
                                arr = btn_tangfa.getText().toString().split(" ");
                                product = new JSONObject();
                                product.put("productName", getProductName(arr[1], arr[0]));
                                product.put("price", arr[2]+arr[3]);
                                jsonArr.put(product);
                                Log.d("sort", "callback: "+arr[2]+":"+btn_tangfa.getText().toString());
                            }
                            if (btn_ranfa.getText().toString().contains(" ")) {
                                arr = btn_ranfa.getText().toString().split(" ");
                                product = new JSONObject();
                                product.put("productName", getProductName(arr[1], arr[0]));
                                product.put("price", arr[2]+arr[3]);
                                jsonArr.put(product);
                            }
                            if (btn_huli.getText().toString().contains(" ")) {
                                arr = btn_huli.getText().toString().split(" ");
                                product = new JSONObject();
                                product.put("productName", getProductName(arr[1], arr[0]));
                                product.put("price", arr[2]+arr[3]);
                                jsonArr.put(product);
                            }
                            if (btn_yangfa.getText().toString().contains(" ")) {
                                arr = btn_yangfa.getText().toString().split(" ");
                                product = new JSONObject();
                                product.put("productName", getProductName(arr[1], arr[0]));
                                product.put("price", arr[2]+arr[3]);
                                jsonArr.put(product);
                            }

                            displayJson.put("bookProducts", jsonArr);
                            PaymentActivity_.intent(BookActivity.this)
                                    .extra("stylistId", id).extra("timeId", dateid).extra("studioId", studioid).extra("comboId", comboID).extra("productIds", getProductID()).extra("opusId", styleid).extra("userCouponId", couponid).extra("integralMoney", getJsonString(html, "integralMoney")).extra("integral", getJsonString(html, "integral")).extra("displayJson", displayJson.toString()).startForResult(REQUEST_CODE_PAYMENT);
                            //// TODO: 2016/3/10
//							BookActivity.this.finish();
//                        } else {
//                            Toast.makeText(aq.getContext(),
//                                    html.getString("errors"), Toast.LENGTH_LONG)
//                                    .show();
//                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        };
        String mobile = pref.loginID().get();
        if (couponid == null) {
            couponid = "";
        }
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();

        pairs.add(new BasicNameValuePair("mobile", mobile));
        pairs.add(new BasicNameValuePair("stylistId", id));
        pairs.add(new BasicNameValuePair("timeId", dateid));
//		pairs.add(new BasicNameValuePair("studioId", studioid));
        pairs.add(new BasicNameValuePair("comboId", comboID));
        pairs.add(new BasicNameValuePair("productIds", getProductID()));
        //pairs.add(new BasicNameValuePair("opusId", styleid));
        pairs.add(new BasicNameValuePair("userCouponId", couponid));

        CommonUtils.postRequest(obtain_book_points, aq, submitCallback, pairs);

    }

    String getProductName(String arg1, String arg2) {
        StringBuilder sb = new StringBuilder();
        sb.append(arg1);
        if(arg2!=null&&!arg2.equals("")) {
            sb.append(" (");
            sb.append(arg2);

        sb.append(")");
        }
        return sb.toString();
    }

    private String getProductID() {
        StringBuilder sb = new StringBuilder();
        if (!"".equals(baseid)) {
            sb.append(baseid);
            sb.append(",");
        }
        if (!"".equals(tangfaid)) {
            sb.append(tangfaid);
            sb.append(",");
        }
        if (!"".equals(ranfaid)) {
            sb.append(ranfaid);
            sb.append(",");
        }
        if (!"".equals(yangfaid)) {
            sb.append(yangfaid);
            sb.append(",");
        }

        sb.append(huliid);
        if (sb.lastIndexOf(",") == sb.length() - 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    private String getCallbackText(String... s) {
        StringBuilder sb = new StringBuilder();
        for (String temp : s) {
            sb.append(temp);
            sb.append(" ");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    @Click
    void btn_back() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent data = new Intent();
        setResult(RESULT_CANCELED, data);
        this.finish();
    }
}
